package com.jlastu.znb.view;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jlastu.znb.R;
import com.jlastu.znb.utils.DatabaseHelper;

public class RegisterActivity extends AppCompatActivity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private EditText confirmPasswordEditText;
    private RadioGroup genderRadioGroup;
    private Button registerButton;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register); // 确保这里的布局文件名与你的XML文件名匹配

        // 初始化控件
        usernameEditText = findViewById(R.id.username);
        passwordEditText = findViewById(R.id.password);
        confirmPasswordEditText = findViewById(R.id.sure_password);
//        genderRadioGroup = findViewById(R.id.genderRadioGroup);
        registerButton = findViewById(R.id.register);

        // 设置注册按钮的点击监听器
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });
    }

    private void registerUser() {
        // 获取用户输入的数据
        String username = usernameEditText.getText().toString().trim();
        String phone = passwordEditText.getText().toString().trim();
        String password = confirmPasswordEditText.getText().toString().trim();

        if (username.isEmpty()){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.isEmpty()){
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.isEmpty()){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 保存注册信息到SharedPreferences
        SharedPreferences prefs = getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("Username", username);
        editor.putString("Password", password);
        editor.putString("phone", phone);
        editor.apply();

        DatabaseHelper dbHelper = new DatabaseHelper(this);

        // 插入用户数据
        dbHelper.insertUser(username, phone, password);

        // 注册成功的提示
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        // 关闭当前活动
        finish();
    }
}